'''
Functions:
1) Multiprocess run
2) Run in batch
3) Don't have to login for each element in list

* excel reader

May need further:
1) feedback of changes.

'''
#coding=utf-8
import time  
from selenium import webdriver  
from selenium.webdriver.common.action_chains import ActionChains

import math
import random
import numpy as np
import os
import xlrd
import pandas as pd

import multiprocessing as mp



def batch_update(account_list, MCC_list):
    
    # log in
    driver = webdriver.Chrome(r'C:\Users\guosu\Desktop\chromedriver_win32\chromedriver.exe')
    elem = driver.get('https://admin.paypal.com')
    elem = driver.find_element_by_id("id_accountNo")
    elem.send_keys(account_list[0])
    elem_search = driver.find_element_by_class_name("findUserSearch").click()

    # define the update function.
    def update(account_id,MCC):

        print("Updating {} time, on account {}".format(str(i+1),MCC))
            
        elem = driver.get('https://admin.paypal.com/cgi-bin/admin?node=supplemental_merchant_info&account_number='+str(account_id))
        driver.execute_script("window.scrollTo(0, (document.body.scrollHeight)/3);")

        js = "document.getElementById('admin_mcc_toggle_status_val').style.display='none';"
        js1 = "document.getElementById('admin_mcc_dynamic_search').style.display='block';"
        js2 = "document.getElementById('admin_mcc').style.display='inline-block';"
        js3 = "document.getElementById('admin_mcc_toggle').style.display='block';"
        js4 = "document.getElementById('admin_mcc_toggle_save').style.display='inline-block';"
        driver.execute_script(js)
        driver.execute_script(js1)
        driver.execute_script(js2)
        driver.execute_script(js3)
        driver.execute_script(js4)
        time.sleep(2)
        driver.find_element_by_id("admin_mcc").send_keys(MCC)
        driver.find_element_by_id("memo_admin_mcc_toggle").send_keys('MCC PROJECT')
        driver.find_element_by_id("admin_mcc_toggle_save").click()
        time.sleep(3)

    for i in range(len(account_list)):
        update(account_list[i], MCC_list[i])
        
    driver.quit()


# get list from excel
def excel_reader(excel_name):
        
    df = pd.read_excel(excel_name, sheet_name=0)     # add sheetname in so we can put all data in one excel
    account_list = list()
    MCC_list = list()
    print(df)
    i = 0
    while (i < len(df)) :
        account_list.append(df.iloc[i][0].tolist())
        MCC_list.append(df.iloc[i][1].tolist())
        i = i + 1
    i = 0
    return account_list, MCC_list

a,mcc1 = excel_reader("2.xlsx")
b,mcc2 = excel_reader("3.xlsx")

#multiprocess run it
if __name__=='__main__':
    # check if the list is correct and then run the main function.
    if((True not in [np.isnan(i) for i in mcc1])and(True not in [np.isnan(i) for i in a])):
        if((True not in [np.isnan(i) for i in mcc2])and(True not in [np.isnan(i) for i in b])):
            #b = "https://admin.paypal.com/cgi-bin/admin?node=supplemental_merchant_info&account_number="+str(a)+"#admin_restrictions_new"
            q = mp.Queue()
            p1 = mp.Process(target=batch_update,args=(a,mcc1))
            p2 = mp.Process(target=batch_update,args=(b,mcc2))
            p1.start()
            p2.start()
            p1.join()
            p2.join()

else:
    print("Sorry, please check your list and make sure each accounts has an MCC code")




#ActionChains(driver).move_by_offset(200, 100).click().perform()   #Click on some elements.
